package com.example.webserver.controller;


import com.example.webserver.entity.Article;
import com.example.webserver.entity.Result;
import com.example.webserver.service.impl.Aritcleserviceimpl;
import com.example.webserver.entity.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("article")
public class Articlecontroller {
    @Autowired
    Aritcleserviceimpl aritcleservice;

    @PostMapping
    public Result addArticle(@RequestBody @Valid Article article){
        aritcleservice.add(article);
        return  Result.success();
    }

    @GetMapping
    public Result<PageBean<Article>> list(
            Integer PageNum,
            Integer PageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state ){
            PageBean<Article> rs=aritcleservice.list(categoryId,state,PageNum,PageSize);
            return Result.success(rs);
    }

    @PutMapping
    public Result update(@RequestBody @Valid Article article){
        aritcleservice.update(article);
        return Result.success();
    }

    @DeleteMapping
    public Result delete(Integer id){
        aritcleservice.delete(id);
        return Result.success();
    }
}
